// 1.工程化与模块化
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
// 2、创建和使用
const router = new VueRouter({
    routes: [{
            name: 'login',
            path: '/login',
            component: () => import('@/views/login.vue')
        },
        {
            name: 'index',
            path: '/index',
            component: () => import('@/views/index.vue'),
            redirect: {
                name: 'hello',
            },
            children: [{
                    name: 'hello',
                    path: 'hello',
                    component: () => import('@/views/hello.vue'),
                },
                {
                    name: 'postList',
                    path: 'postList',
                    component: () => import('@/views/postList.vue'),
                },
                {
                    name: 'postPublish',
                    path: 'postPublish',
                    component: () => import('@/views/postPublish.vue'),
                },
            ]
        }
    ]
})

// 导航守卫
router.beforeEach((to, from, next) => {
    if (to.path == '/login') {
        next()
    } else {
        let token = localStorage.getItem('heima_back_token_61')
        if (token) {
            next()
        } else {
            next('/login')
        }
    }
})
// 3、暴露
export default router